#include "sessionfriendarea.h"
#include<QVBoxLayout>
#include<QPushButton>
#include<QGridLayout>
#include<QLabel>
#include<QLayoutItem>
#include<QStyleOption>
#include<QPainter>
#include"debug.h"
SessionFriendArea::SessionFriendArea(QWidget *parent)
    : QScrollArea{parent}
{
    //开启滚动条
    this->setWidgetResizable(true);
    //设置垂直滚动条样式
    this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical {width:2px; background-color:gdb(46,46,46);}");
    //设置水平滚动条
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal {height: 0px;}");
    //实例化窗口对象container
    this->container = new QWidget();
    this->container->setFixedWidth(260);
    this->setWidget(container);

    QVBoxLayout* layout = new QVBoxLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
    layout->setAlignment(Qt::AlignTop);//向上对齐
    container->setLayout(layout);
//测试会话区域
#if __TEST__UI__
    QIcon avater(":/resource/image/avater.png");
    for(int i = 0;i<5;++i)
    {
        this->addItem(avater,"好友" + QString::number(i),"消息" + QString::number(i));
    }
#endif
}

void SessionFriendArea::addItem(const QIcon &avater, const QString &name, const QString &text)
{
    SessionItem* item = new SessionItem(this,avater,name,text);
    container->layout()->addWidget(item);
}

void SessionFriendArea::clear()
{
    //获取布局管理器对象,获取的是父类管理器对象
    QLayout* layout = container->layout();
    int cnt = layout->count();
    //方案1：从后往前删除，正向删除会导致下标失效！
    for(int i = cnt-1;i >= 0;--i)
    {
        //移除指定下标的布局管理器中的元素
        QLayoutItem* item = layout->takeAt(i);
        if(item->widget())
        {
            //删除元素中的窗口对象
            delete item->widget();
        }
    }
}

//重写该方法保证QSS能执行
void SessionItem::paintEvent(QPaintEvent *e)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

SessionItem::SessionItem(QWidget *owner, const QIcon &avater, const QString &name, const QString &text)
    :owner(owner)
{
    //设置当前元素整体窗口的高度
    this->setFixedHeight(70);
    //设置颜色
    this->setStyleSheet("QWidget { background-color: rgb(231,231,231); }");
    //初始化布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(0);
    layout->setContentsMargins(0,0,0,0);
    this->setLayout(layout);

    //设置头像布局
    QPushButton* avaterBtn = new QPushButton();
    avaterBtn->setFixedSize(50,50);
    avaterBtn->setIconSize(QSize(50,50));
    avaterBtn->setIcon(avater);
    //去除边框
    avaterBtn->setStyleSheet("QPushButton { border:none; }");
    avaterBtn->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
    //=============================
    //设置昵称布局，使用label
    QLabel* nameLabel = new QLabel();
    nameLabel->setText(name);
    //设置字体样式
    nameLabel->setStyleSheet("QLabel {font-size: 18px; font-weight:600; }");
    //名字标签只占上半部分
    nameLabel->setFixedHeight(35);
    //设置布局策略:水平舒展，垂直固定
    nameLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    //========================================
    //设置最后一条显示的消息内容 or 好友签名
    QLabel* textLabel = new QLabel();
    textLabel->setText(text);
    textLabel->setFixedHeight(35);
    textLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    //添加进布局管理器
    layout->addWidget(avaterBtn,0,0,2,2);
    layout->addWidget(nameLabel,0,2,1,1);
    layout->addWidget(textLabel,1,2,1,1);

}
